<?php

/**
 * uniBoard Cache Class
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-06 01:45:14 +0000 (Mon, 06 Sep 2010) $
 * $Revision: 59 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class class_cache{
	
	/**
	 * Cache load failed?
	 *
	 * @var bool
	 */
	
	static public $load_error = false;
	
	// ------------------------------------------------------------------------

	/**
	 * Saves data to cache file
	 *
	 * @param string $file_name
	 * @param mixed $cache_data
	 * @param integer $cache_timeout
	 */
	
	static function saveCache( $file_name, $cache_data, $cache_timeout = 0){

		// Start creating file
		$cache_file = '<?php

';		
		
		// Generate stamp
		$cache_file .= self::makeFileStamp( 'RAW PHP DATA');
		
		// Include timeout?
		if ( $cache_timeout > 0)
		{
			$cache_file .= '$cache_timeout = ' . ( time() +  $cache_timeout ) . ';

';
		}
		
		// Put in cache data
		$cache_file .= '$cache_data = ' . var_export( $cache_data, true) . ';';
		
		// Save file
		file_put_contents( CACHE_PATH . $file_name . PHP_EXT, $cache_file);
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Loads cache from file
	 *
	 * @param string $file_name
	 * @return mixed
	 */
	
	static function loadCache( $file_name){
		
		// Reset state to false
		self::$load_error = true;
		
		// File exists?
		if ( file_exists( CACHE_PATH . $file_name . PHP_EXT))
		{
			// Include file
			include( CACHE_PATH . $file_name . PHP_EXT);
			
			// Timeout is not set or in future
			if ( !isset( $cache_timeout) || $cache_timeout > time())
			{
				// Load succeeded
				self::$load_error = false;
				
				// Return cached data
				return $cache_data;
			}
			else{
				// Return null
				return NULL;
			}
		}
		else
		{
			// Return null
			return NULL;
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Deletes cache file
	 *
	 * @param string $file_name
	 */
	
	static function flushCache( $file_name){
		
		// If file exists, delete it
		if ( file_exists( CACHE_PATH . $file_name . PHP_EXT))
		{
			unlink( CACHE_PATH . $file_name . PHP_EXT);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Generates cache file stamp, thats localisated at beginning of every cache file
	 *
	 * @param string $file_type
	 * @param bool $stamp_php
	 * @return string
	 */
	
	static function makeFileStamp( $file_type, $stamp_php = true){
		
		// Stamp php?
		if ( $stamp_php)
		{
			// Return php type cache
			return '/**
 * ' . PRODUCT_NAME . ' CACHE FILE: ' . $file_type . '
 *
 * GENERATED: ' . gmdate( 'r') . '
 */ 

// ------------------------------------------------------------------------

';
		}
		else
		{
			// @TODO: text files stamp
			return '/*
	' . PRODUCT_NAME . ' CACHE FILE
	' . $file_type . '
	GENERATED: ' . gmdate( 'r') . '
*/ 
 
';
		}
		
	}
	
}